﻿#region File Description
/*-----------------------------------------------------------------------------
 * Class: BrickWall
 *
 * BrickWall is an obstacle Ron must defeat in order to get a Nacho.
 * Ron can break the brick wall by burping fire on it.
 * 
 -------------------------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;



namespace _2DProject
{
    class BrickWall : DrawableGameComponent
    {
        //used for Game states (title screen, game mode, ending mode)
        enum GameState
        {
            TitleScreen = 0,
            GameStarted,
            GameEnded,
        }

        GameState gameState = GameState.TitleScreen;
        
        public BrickWall(Game g, Vector2 pos)
            : base(g)
        {
            spritePosition = pos;   //initial position
            destroyed = false;      //checks if the wall still exists
        }

        //--- Public getters/setters for member variables ---//
        public Vector2 Position
        {
            get { return spritePosition; }
            set { spritePosition = value; }
        }

        public Boolean IsDestroyed
        {
            get { return destroyed; }
            set { destroyed = value; } 
        }

        public float Height
        {
            get { return wallTexture.Height; }
        }
        
        public float Width
        {
            get { return wallTexture.Width; }
        }


        //--- Member variables are always private ---//
        private Vector2 spritePosition;
       // private String imgName;
        private Texture2D wallTexture;
        private SpriteBatch spriteBatch;

        private bool destroyed; //keeps track of whether or not this obstacle has been destroyed




        // Called when the object is created in Game's Initialize() method
        // Reads in whatever resources are needed for the object
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            wallTexture = Game.Content.Load<Texture2D>("brickwall");
        }


        public override void Draw(GameTime gameTime)
        {
            if (gameState == GameState.GameStarted)
            {
                Vector2 offset = new Vector2(wallTexture.Width / 2, wallTexture.Height / 2);

                spriteBatch.Begin();                                            // Begin the batch
                spriteBatch.Draw(wallTexture, spritePosition - offset, Color.White); // Draw to the batch
                spriteBatch.End();                                              // end the batch,
                // causing it to render the sprite to the screen
            }
        }


        public override void Update(GameTime gameTime)
        {
            // if the wall has been destroyed don't do all this!
            if (destroyed)
                Game.Components.Remove(this);

            if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                gameState = GameState.GameStarted;

            // -- Janky collision detection --//

            // If a character runs into the wall, shift their x position back (keeps them from getting through)
            foreach (var component in Game.Components)
            {
                Character c = component as Character;
                if (c != null)
                {
                    if ((Position.X - wallTexture.Width / 2 < c.Position.X) && (c.Position.X < Position.X + wallTexture.Width / 2) &&
                        (Position.Y - wallTexture.Height / 2 < c.Position.Y) && (c.Position.Y < Position.Y + wallTexture.Height / 2))
                    {
                        // Shift the character backaway from wall (right) so that they can't get through it until it is destroyed
                        c.Position = new Vector2(c.Position.X + 10f, c.Position.Y);
                    }
                }
            }


        }
    }
}
